Skip to content

ref(mcp): Replace instance patches with middleware - #6764

Open
alexander-alderman-webb wants to merge 41 commits into
masterfrom
webb/mcp/use-middleware-5
Open

ref(mcp): Replace instance patches with middleware#6764
alexander-alderman-webb wants to merge 41 commits into
masterfrom
webb/mcp/use-middleware-5

Conversation

@alexander-alderman-webb

@alexander-alderman-webb alexander-alderman-webb commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Description

Add a middleware instead of wrapping handlers on registration.
All requests go through the middleware, whereas there are multiple ways to register a handler.

Issues

Closes #6986

Reminders

@alexander-alderman-webb
alexander-alderman-webb changed the base branch from webb/mcp/use-middleware-4 to webb/mcp/prompt-arguments July 7, 2026 09:35
@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Codecov Results 📊

94982 passed | ⏭️ 6343 skipped | Total: 101325 | Pass Rate: 93.74% | Execution Time: 343m 9s

📊 Comparison with Base Branch

Metric Change
Total Tests
Passed Tests 📈 +1
Failed Tests 📉 -1
Skipped Tests

➖ Removed Tests (1)

View removed tests
  • test_continuous_profiler_auto_start_and_stop_sampled_span_streaming[non-experiment-thread]
    • File: tests.profiler.test_continuous_profiler

All tests are passing successfully.

✅ Patch coverage is 83.66%. Project has 2523 uncovered lines.
❌ Project coverage is 89.86%. Comparing base (base) to head (head).

Files with missing lines (1)
File Patch % Lines
sentry_sdk/integrations/mcp.py 83.66% ⚠️ 25 Missing and 25 partials
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
- Coverage    89.88%    89.86%    -0.02%
==========================================
  Files          193       193         —
  Lines        24832     24889       +57
  Branches      8912      8954       +42
==========================================
+ Hits         22319     22366       +47
- Misses        2513      2523       +10
- Partials      1416      1440       +24

Generated by Codecov Action

@alexander-alderman-webb
alexander-alderman-webb marked this pull request as ready for review July 7, 2026 11:39
@alexander-alderman-webb
alexander-alderman-webb requested a review from a team as a code owner July 7, 2026 11:39
Comment thread sentry_sdk/integrations/mcp.py Outdated
Comment thread sentry_sdk/integrations/mcp.py
Comment thread sentry_sdk/integrations/mcp.py Outdated
Comment thread sentry_sdk/integrations/mcp.py
Comment thread sentry_sdk/integrations/mcp.py

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 5477c8e. Configure here.

Comment thread sentry_sdk/integrations/mcp.py
Comment thread sentry_sdk/integrations/mcp.py Outdated
Base automatically changed from webb/mcp/prompt-arguments to master July 28, 2026 11:52

uri = None
if ctx.params is not None:
uri = getattr(ctx.params, "uri", None)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: In _instrument_v2_resource_read, getattr(ctx.params, "uri", None) is used on a dict-like object, which will always return None, making the intended URI parsing logic dead code.
Severity: LOW

Suggested Fix

Replace the incorrect attribute access with dictionary-style access. Change uri = getattr(ctx.params, "uri", None) to uri = ctx.params.get("uri") to be consistent with the access patterns used elsewhere in the middleware for the ctx.params object.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: sentry_sdk/integrations/mcp.py#L950

Potential issue: In the `_instrument_v2_resource_read` middleware, `ctx.params` is a
dictionary-like object containing raw request parameters. However, the code attempts to
retrieve the URI using `getattr(ctx.params, "uri", None)`, which is intended for objects
with attributes. This call will always return `None` because dictionaries do not have a
`.uri` attribute. As a result, the more robust URI scheme extraction logic that relies
on a parsed URI object is never executed. While a fallback mechanism using string
parsing handles most standard URIs, the intended, more reliable path is effectively dead
code. This is inconsistent with other parts of the code that correctly use
dictionary-style access like `ctx.params.get("uri")`.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing a pass now to reduce dead code, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use MCP middleware

2 participants